package com.fwzc.giuseppe.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.Api.Api;
import com.fwzc.giuseppe.adapter.HolderAdapter;
import com.fwzc.giuseppe.adapter.HolderAdapter.ViewCreator;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.config.Config;

public class ShoppingCart extends BaseActivity implements OnClickListener{
	private Context context = ShoppingCart.this;
	private ListView lv;
	private List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>(); 
	private HolderAdapter<HashMap<String, String>> mAdapter;
	
	private Button addBt,subBt;
	private EditText Snum;
	private TextView add_price,tv_price;
	int num = 1;
	float price;
	float result;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shoppingcart);
		initBar();
		actionbar_side_name.setText("购物车");
		bg_actionbar_side.setBackgroundColor(getResources().getColor(R.color.transparent));
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		iv_actionbar_side_left.setOnClickListener(this);
		initView();
	}

	private void initView() {

//		subBt = (Button) findViewById(R.id.Btn_Reduction);//-
//		Snum = (EditText) findViewById(R.id.shopping_num);//件数
//		addBt = (Button) findViewById(R.id.Btn_Plus);//+
//		
//		addBt.setOnClickListener(new OnButtonClickListener());
//		Snum.addTextChangedListener(new OnTextChangeListener());
//		subBt.setOnClickListener(new OnButtonClickListener());
//		addBt.setTag("+");
//		subBt.setTag("-");
//		//设置输入类型为数字
//		Snum.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
//		Snum.setText(String.valueOf(num));
		
		tv_price = (TextView) findViewById(R.id.tv_price);//单价
		add_price = (TextView) findViewById(R.id.add_price);//总价

		

		lv = (ListView) findViewById(R.id.shopping_listview);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(); 
				intent.setClass(context, PayActivity.class);
				context.startActivity(intent);
			}
		});
		
		createAdapter();
		if (Api.false_data) {
			setFalseData();
		} else {

			loadingShoppingCart();
		}
	}	
	

	private void createAdapter() {
		ViewCreator<HashMap<String, String>> creator = new ViewCreator<HashMap<String, String>>() {
			@SuppressLint("InflateParams")
			@Override
			public View createView(LayoutInflater inflater, int position,
					HashMap<String, String> map) {
				
				return LayoutInflater.from(context).inflate(
						R.layout.item_list_shopping, null);
			}  

			@Override
			public void updateView(View view, int position,
					final HashMap<String, String> map) {

				ImageView picture =(ImageView) view.findViewById(R.id.img_parents);
				TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
				TextView order_num = (TextView) view.findViewById(R.id.order_num);
				tv_price = (TextView) view.findViewById(R.id.tv_price);
				
				if(Api.false_data){
					
					picture.setBackgroundResource(R.drawable.empty_photo);
				}else{
					
					imageLoader.displayImage(map.get("imageUrl"), picture,Config.no_iv_option);
				}
				tv_name.setText(map.get("name"));
				order_num.setText("订单号"+map.get("order"));
				tv_price.setText("￥"+map.get("price"));
			
			}
		};

		 mAdapter = new HolderAdapter<HashMap<String,String>>(this.getLayoutInflater(),creator);
		 lv.setAdapter(mAdapter);
		 mAdapter.update(data);
		
	}

	private void loadingShoppingCart() {
		// TODO Auto-generated method stub
		
	}

	private void setFalseData() {

		        data.clear();
				
		        HashMap<String,String> infomap = new HashMap<String,String>(); 

				infomap.put("name", "日炎斗篷");
				infomap.put("order", "1314257758");
				infomap.put("price", "2600");
				data.add(infomap);
				
				HashMap<String,String> infomap1 = new HashMap<String,String>(); 
			
				infomap1.put("name", "荆棘之甲");
				infomap1.put("order", "1314257758");
				infomap1.put("price", "2300");
				
				HashMap<String,String> infomap2 = new HashMap<String,String>(); 

				infomap2.put("name", "狂徒铠甲");
				infomap2.put("order", "1314257758");
				infomap2.put("price", "2900");
				data.add(infomap2);
							
				mAdapter.update(data);

		
	}
	/**
	class OnButtonClickListener implements OnClickListener
	{

		@Override
		public void onClick(View v)
		{
			String numString = Snum.getText().toString();
			if (numString == null || numString.equals(""))
			{
				num = 1;
				Snum.setText("1");
			} else
			{
				if (v.getTag().equals("+"))
				{
					if (++num <= 1)  
					{
						num--;
						showToast("逗！");
					} else
					{
						Snum.setText(String.valueOf(num));
					}
				} else if (v.getTag().equals("-"))
				{
					if (--num < 1)  
					{
						num++;
						showToast("逗！");
					} else
					{
						Snum.setText(String.valueOf(num));

					}
				}
			}
		}
	}
	class OnTextChangeListener implements TextWatcher
	{

		@Override
		public void afterTextChanged(Editable s)
		{
			String numString = s.toString();
			if(numString == null || numString.equals(""))
			{
				num = 1;

			}
			else {
				int numInt = Integer.parseInt(numString);
				if (numInt < 1)
				{
					num++;
					showToast("逗！");
				} else
				{
					//设置EditText光标位置 为文本末端
					Snum.setSelection(Snum.getText().toString().length());
					num = numInt;

				}
			}
		}
		//结算
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after)
		{

		}
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count)
		{
		    result = num * price;
		    add_price.setText("￥"+result);
		}
		
	}*/
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 返回
		case R.id.iv_actionbar_side_left:
			finish();
			break;

		default:
			break;
		}
	}
}
